package homework7.soso.service.impl;

import homework7.soso.entity.MobileCard;
import homework7.soso.service.CallService;
import homework7.soso.service.NetService;
import homework7.soso.service.SendService;

/**
 * @author fengq
 * 超人套餐
 */
public class SuperPackage extends ServicePackage implements CallService, NetService, SendService {
    /**
     * 通话时长
     */
    private int talkTime;
    /**
     * 短信条数
     */
    private int smsCount;
    /**
     * 上网流量
     */
    private int flow;

    @Override
    public void showInfo() {
        System.out.println("超人套餐:通话时长为200分钟/月，短信条数为50条/ 月，上网流量1GB/月");
    }

    public SuperPackage(double price, int talkTime, int smsCount, int flow) {
        this.price=price;
        this.talkTime = talkTime;
        this.smsCount = smsCount;
        this.flow = flow;
    }

    public SuperPackage(int talkTime, int smsCount, int flow) {
        this.talkTime = talkTime;
        this.smsCount = smsCount;
        this.flow = flow;
    }

    public int getTalkTime() {
        return talkTime;
    }

    public void setTalkTime(int talkTime) {
        this.talkTime = talkTime;
    }

    public int getSmsCount() {
        return smsCount;
    }

    public void setSmsCount(int smsCount) {
        this.smsCount = smsCount;
    }

    public int getFlow() {
        return flow;
    }

    public void setFlow(int flow) {
        this.flow = flow;
    }

    @Override
    public int call(int minCount, MobileCard card) throws Exception {
        SuperPackage superPackage = (SuperPackage) card.getServicePackage();
        int time = minCount;
        for (int i = 0; i < minCount; i++) {
            //套餐剩余通话时长大于一分钟
            if (superPackage.getTalkTime()-card.getRealTalkTime()>0) {

                card.setRealTalkTime(card.getRealTalkTime()+1);
            }else if (card.getMoney()>=0.2) {
                //账户余额大于0.2yuan

                card.setMoney(card.getMoney()-0.2);
                card.setConsumeAmount(card.getConsumeAmount()+0.2);
            }else {
                time = i;
                card.setMoney(time);
                throw new Exception("本次通话"+time+"分钟，您的余额不足，请充值后再使用");
            }

        }
        return time;
  }

    @Override
    public int netPlay(int flow, MobileCard card) throws Exception {
        SuperPackage superPackage = (SuperPackage) card.getServicePackage();
        int spend = flow*1024;
        for (int i = 0; i < flow*1024; i++) {
            if ((superPackage.getFlow())*1024-card.getRealFlow()>0) {

                card.setRealFlow(card.getRealFlow()+1);
            }else if (card.getMoney()>=0.1) {

                card.setMoney(card.getMoney()-0.1);
                card.setConsumeAmount(card.getConsumeAmount()+0.1);
            }else {
                spend = i;
                card.setMoney(spend);
                throw new Exception("本次上网流量"+spend+"MB，您的余额不足，请充值后使用");
            }

        }
        return spend;
    }

    @Override
    public int send(int count, MobileCard card) throws Exception {
    SuperPackage superPackage = (SuperPackage) card.getServicePackage();
    int time = count;
        for (int i = 0; i < count; i++) {
        if (superPackage.getSmsCount() - card.getRealSMSCount() > 0) {

        card.setRealSMSCount(card.getRealSMSCount() + 1);
        } else if (card.getMoney() >= 0.1) {

        card.setMoney(card.getMoney() - 0.1);
        card.setConsumeAmount(card.getConsumeAmount() + 0.1);
        } else {
        time = i;
        card.setMoney(time);
        throw new Exception("本次发信息" + time + "条，您的余额不足，请充值后使用");
        }
        }
        return time;
        }
        }
